<?php

namespace App\Listeners\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\UploadFileResourceEnum;
use App\Events\OpenShop\GenerateHeatMapImageEvent;
use App\Listeners\BaseListener;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUploadFileResourceModel;
use App\Service\Common\HeatMapService;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Kuafu\File\FileUtil;

class GenerateHeatMapImageListener extends BaseListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Handle the event.
     *
     * @param GenerateHeatMapImageEvent $event
     * @return void
     * @throws Exception
     */
    public function handle(GenerateHeatMapImageEvent $event)
    {
        Log::channel('listener')->info(__CLASS__ . 'make heatmap');
        //生成热力图
        $heatMapImage = HeatMapService::makeHeatMap($event->backgroundPath, $event->lng, $event->lat);
        //删除背景图片
        @unlink($event->backgroundPath);
        //上传OSS
        $heatMap = FileUtil::uploadFile($heatMapImage);
        //入库
        OpenShopUploadFileResourceModel::query()->insert([
            "orderNo"   => "FY" . $event->siteLocationId,
            "position"  => UploadFileResourceEnum::TAKE_OUT_HEATMAP,
            "url"       => $heatMap['path'],
            "isPublic"  => 1,
            "createdAt" => time(),
        ]);
        @unlink($heatMapImage);
    }
}
